package com.cw.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cw.mapper.TbGoodsDescMapper;
import com.cw.mapper.TbGoodsMapper;
import com.cw.mapper.TbItemMapper;
import com.cw.pojo.TbGoods;
import com.cw.pojo.TbGoodsDesc;
import com.cw.pojo.TbItem;
import com.cw.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;

    @Autowired
    private TbItemMapper itemMapper;

    @Override
    @Transactional
    public void saveGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, List<TbItem> itemList, String name, String thp1, String thp2) {

        tbGoodsMapper.saveGoods(tbGoods);

        tbGoodsDesc.setGoodsId(tbGoods.getId());

        tbGoodsDescMapper.insertSelective(tbGoodsDesc);

        for (int i = 0; i < itemList.size(); i++) {

            TbItem ti = itemList.get(i);

            String spec = ti.getSpec();

            String thp11 = spec.replace("thp1", thp1);

            if (thp2 != null && thp2 != "") {

                String thp21 = thp11.replace("thp2", thp2);

                ti.setSpec(thp21);
            }else {

                ti.setSpec(thp11);
            }

            ti.setSellerId(tbGoods.getSellerId());

            ti.setSeller(name);

            itemMapper.insertSelective(ti);
        }
    }

    @Override
    public List<TbGoods> getGoodsInfo(String goodsName) {
        return tbGoodsMapper.getGoodsInfo(goodsName);
    }

    @Override
    public void checkStatu(Long[] ids, String status) {
        tbGoodsMapper.checkStatu(ids, status);
    }

    @Override
    public TbGoods getGoods(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public TbGoodsDesc getGoodsDesc(Long goodsId) {
        return tbGoodsDescMapper.selectByPrimaryKey(goodsId);
    }
}
